﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KVB.Common;
using Customer_Profile.Admin;

namespace Customer_Profile.ClientManage
{
    public partial class CustomerAdd : System.Web.UI.Page
    {
        Customer model = new Customer();
        CustomerDao da = new CustomerDao();
        string operatorname = "";
        public string action = "Customer Add(<span style=\"color:Green\">Please add the customer in here</span>)";
        public string sGuid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["usermodel"] != null)
            {
                operatorname = ((UserInfoEntity)Session["usermodel"]).UserName;                
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["Guid"] != null)
                    {
                        ViewState["Guid"] = Request.QueryString["Guid"].ToString();
                        SetOldData(ViewState["Guid"].ToString());
                    }
                    else
                    {
                        Comm.BindDll(ref this.ddllocal, "Local", false, "");
                        Comm.BindDll(ref this.ddlstatus, "status", false, "");
                        Comm.BindDll(ref this.ddlAdvisor, "Advisor", false, "");
                        this.txtbirthday.Value = "1980-01-01";
                        this.txtAuthoriseBirth.Value = "1980-01-01";
                    }
                }
                if (ViewState["Guid"] != null)
                {
                    sGuid = ViewState["Guid"].ToString();
                }
            }
            else
            {
                Response.Redirect("/login.aspx", true);
            }
        }

        private void SetOldData(string Guid)
        {
            try
            {
                action = "Customer Edit(<span style=\"color:Green\">Please modify the customer in here</span>)";
                model = da.GetModel(Guid);
                Comm.BindDll(ref this.ddllocal, "Local", false, model.Location);
                Comm.BindDll(ref this.ddlstatus, "status", false, model.Status);
                Comm.BindDll(ref this.ddlAdvisor, "Advisor", false, model.Advisor);
                this.txtfirstname.Value = model.FirstName;
                this.txtlastname.Value = model.LastName;
                this.ddltitle.SelectedIndex = this.ddltitle.Items.IndexOf(this.ddltitle.Items.FindByValue(model.Title));
                this.txtcity.Value = model.City;
                this.txtprovince.Value = model.RegionProvince;
                this.txtCountry.Value = model.Country;
                this.txthometel.Value = model.HomeNo;
                this.txtworktel.Value = model.WorkNo;
                this.txtfax.Value = model.FaxNo;
                this.txtemail.Value = model.Email;
                this.txtaddress.Value = model.Address;
                this.txtpostcode.Value = model.PostalCode;
                this.ddlAdvisor.SelectedIndex = this.ddlAdvisor.Items.IndexOf(this.ddlAdvisor.Items.FindByValue(model.Advisor));
                this.txtbirthday.Value = model.Birthday;
                this.ddlstatus.SelectedIndex = this.ddlstatus.Items.IndexOf(this.ddlstatus.Items.FindByValue(model.Status));
                this.ddllocal.SelectedIndex = this.ddllocal.Items.IndexOf(this.ddllocal.Items.FindByValue(model.Location));
                this.txtibid.Value = model.IBID;
                this.txtibloginname.Value = model.IBLoginName;
                this.txtphonepassword.Value = model.PhonePassword;
                this.txtremarks.Text = model.Remarks;
                this.txtfinno.Value = model.FINNo;
                this.txtcsnno.Value = model.CSNNo;
                this.ddlauthorisetitle.SelectedIndex = this.ddlauthorisetitle.Items.IndexOf(this.ddlauthorisetitle.Items.FindByValue(model.authorise_Title));
                this.TxtAuthoriseFirstName.Value = model.authorise_FirstName;
                this.txtAuthoriseLastName.Value = model.authorise_LastName;
                this.txtAuthoriseHomeNo.Value = model.authorise_HomeNO;
                this.txtAuthoriseWorkNo.Value = model.authorise_WorkNO;
                this.txtAuthoriseBirth.Value = model.authorise_Birth;
                this.txtAuthoriseAddress.Value = model.authorise_Address;
                this.txtAuthoriseCountry.Value = model.authorise_Country;
                this.txtAuthoriseCity.Value = model.authorise_City;
                this.txtAuthoriseProvince.Value = model.authorise_Province;
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("SetOldData", ex.ToString());
            }
        }

        protected void save_ServerClick(object sender, EventArgs e)
        {
            //if (ViewState["Guid"] == null)
            //{
            //    if (Request.Form["hiddenflag"] != null && Request.Form["hiddenflag"].ToString() != "YES")
            //    {
            //        Comm.ViewAlertMsg("IB ID Exist or Error");
            //        return;
            //    }
            //    if (Request.Form["hiddenflagname"] != null && Request.Form["hiddenflagname"].ToString() != "YES")
            //    {
            //        Comm.ViewAlertMsg("IB login Name Exist or Error");
            //        return;
            //    }
            //}
            try
            {                
                model.FirstName = this.txtfirstname.Value.Trim();
                model.LastName = this.txtlastname.Value.Trim();
                model.Title = this.ddltitle.SelectedValue.Trim();
                model.City = this.txtcity.Value.Trim();
                model.RegionProvince = this.txtprovince.Value.Trim();
                model.Country = this.txtCountry.Value.Trim();
                model.HomeNo = this.txthometel.Value.Trim();
                model.WorkNo = this.txtworktel.Value.Trim();
                model.FaxNo = this.txtfax.Value.Trim();
                model.Email = this.txtemail.Value.Trim();
                model.Address = this.txtaddress.Value.Trim();
                model.PostalCode = this.txtpostcode.Value.Trim();
                model.Advisor = this.ddlAdvisor.SelectedValue.Trim();
                model.Birthday = this.txtbirthday.Value.Trim();
                model.Status = this.ddlstatus.SelectedValue.Trim();
                model.Location = this.ddllocal.SelectedValue.Trim();
                model.IBID = this.txtibid.Value.Trim();
                model.IBLoginName = this.txtibloginname.Value.Trim();
                model.PhonePassword = this.txtphonepassword.Value.Trim();
                model.Remarks = this.txtremarks.Text.Trim();
                model.CSNNo = this.txtcsnno.Value.Trim();
                model.FINNo = this.txtfinno.Value.Trim();
                model.authorise_Title = ddlauthorisetitle.SelectedValue.ToString().Trim();
                model.authorise_FirstName = TxtAuthoriseFirstName.Value.Trim();
                model.authorise_LastName = txtAuthoriseLastName.Value.Trim();
                model.authorise_HomeNO = txtAuthoriseHomeNo.Value.Trim();
                model.authorise_WorkNO = txtAuthoriseWorkNo.Value.Trim();
                model.authorise_Birth = txtAuthoriseBirth.Value.Trim();
                model.authorise_Address = txtAuthoriseAddress.Value.Trim();
                model.authorise_Country = txtAuthoriseCountry.Value.Trim();
                model.authorise_City = txtAuthoriseCity.Value.Trim();
                model.authorise_Province = txtAuthoriseProvince.Value.Trim();
                if (ViewState["Guid"] != null)
                {
                    model.Guid = ViewState["Guid"].ToString();
                    da.Update(model, operatorname);
                }
                else
                {
                    da.Add(model, operatorname);
                }
                Response.Redirect("CustomerList.aspx", false);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("save_ServerClick", ex.ToString());
                if (ex.Message.IndexOf("with unique index") > 0)//唯一索引
                {
                    if (ex.Message.IndexOf("IBID") > 0)
                    {
                        Comm.ViewAlertMsg("IBID '" + model.IBID + "' existed");
                    }
                    else if (ex.Message.IndexOf("IBLoginName") > 0)
                    {
                        Comm.ViewAlertMsg("IBLoginName '" + model.IBLoginName + "' existed");
                    }
                    if (ViewState["Guid"] != null)
                    {
                        this.action = "Customer Edit";
                    }
                    else
                    {
                        this.action = "Customer Add";
                    }
                }
            }
        }

        protected void btnback_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("CustomerList.aspx", true);
        }
    }
}
